local chaowei = fk.CreateSkill {
  name = "miniex__chaowei",
}

Fk:loadTranslationTable {
  ["miniex__chaowei"] = "朝威",
  [":miniex__chaowei"] = "出牌阶段限一次，你可以摸一张牌并与至多三名角色进行“<a href='zhuluPindian'>逐鹿</a>”：" ..
      "赢的角色使用的下三张【杀】无次数限制，没赢的角色选择交给你一张牌或受到你造成的1点伤害。",

  ["@miniex__chaowei"] = "朝威",
  ["#miniex__chaowei-zhulu"] = "朝威：选择至多三名角色“逐鹿”",
  ["#miniex__chaowei-give"] = "朝威：交给 %src 一张牌或者受到其造成的1点伤害",

  ["$miniex__chaowei1"] = "文以经国，威服天下！",
  ["$miniex__chaowei2"] = "至尊之业，不因情异之，因私废之。"
}

local U = require "packages/utility/utility"
chaowei:addEffect("active", {
  anim_type = "control",
  max_phase_use_time = 1,
  card_filter = Util.FalseFunc,
  card_num = 0,
  min_target_num = 1,
  max_target_num = 3,
  target_filter = function(self, player, to_select, selected, selected_cards, card, extra_data)
    return #selected < 3 and player:canPindian(to_select, true)
  end,
  can_use = function(self, player)
    return not not table.find(Fk:currentRoom().alive_players, function(p, index, array)
      return player:canPindian(p, true)
    end) and player:usedEffectTimes(self.name, Player.HistoryPhase) == 0
  end,
  on_use = function(self, room, effect)
    local skillName = chaowei.name
    local player = effect.from
    player:drawCards(1, skillName)
    if player.dead then return end
    local targets = table.filter(room.alive_players, function(p)
      return player:canPindian(p, true)
    end)
    if #targets == 0 then return end
    local tos = effect.tos
    local pindian = U.startZhuLu(player, tos, skillName)
    local winner = pindian.winner
    if winner then
      room:setPlayerMark(winner, "@miniex__chaowei", 3)
    end
    if player.dead then return end
    table.insert(tos, player)
    table.removeOne(tos, winner)
    room:sortByAction(tos)
    for _, p in ipairs(tos) do
      if player.dead then return end
      local give = false
      if p ~= player and not p:isNude() then
        local cards = room:askToCards(p, {
          min_num = 1,
          max_num = 1,
          skill_name = skillName,
          prompt = "#miniex__chaowei-give:" .. player.id,
          cancelable = true,
          include_equip = true,
        })
        if #cards > 0 then
          give = true
          room:obtainCard(player, cards, false, fk.ReasonGive, player, skillName)
        end
      end
      if not give then
        room:damage {
          from = player,
          to = p,
          damage = 1,
          skillName = skillName,
        }
      end
    end
  end
})

chaowei:addEffect(fk.AfterCardUseDeclared, {
  can_refresh = function(self, event, target, player, data)
    return target == player and player:getMark("@miniex__chaowei") > 0 and data.card.trueName == "slash"
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:removePlayerMark(player, "@miniex__chaowei", 1)
    data.extraUse = true
  end
})

chaowei:addEffect("targetmod", {
  bypass_times = function(self, player, skill, scope, card, to)
    return card and player:getMark("@miniex__chaowei") > 0 and card.trueName == "slash"
  end,
})

return chaowei
